package com.powater.assetmanagementofxinyu.controller;


import com.powater.assetmanagementofxinyu.dto.userinfo.LoginMessageDto;
import com.powater.assetmanagementofxinyu.enums.LoginEnum;
import com.powater.assetmanagementofxinyu.enums.ResultCode;
import com.powater.assetmanagementofxinyu.pojo.User;
import com.powater.assetmanagementofxinyu.service.UserService;
import com.powater.assetmanagementofxinyu.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "UserController",tags = "用户增删改接口")
@RestController
@RequestMapping("/UserOperate")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/Test")
    public String Test(){
        return "okok";
    }



    @ApiOperation(value = "登录",httpMethod = "POST")
    @ApiImplicitParam(name = "loginMessageDTO", value = "该参数用来封装用户登录填入的数据", paramType = "body", dataType = "LoginMessageDTO", required = true)
    @PostMapping("/loginCheck")
    public CommonResult loginCheck(@RequestBody LoginMessageDto loginMessageDto,HttpServletRequest request){
        if ((loginMessageDto.getUserpassword()==null)||(loginMessageDto.getUsername()==null)){
            return new CommonResult(ResultCode.ERROR.getCode(),"输入数据不能为空",null);
        }
        int resultcode=userService.checkUser(loginMessageDto,request);
        if (resultcode== LoginEnum.SUCCESS.getCode()){
            User user=(User) request.getSession().getAttribute("userbean");
            return new CommonResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),loginMessageDto);
        }else if (resultcode==LoginEnum.PASSWORD_ERROR.getCode()){
            return new CommonResult(LoginEnum.PASSWORD_ERROR.getCode(),LoginEnum.PASSWORD_ERROR.getMessage(),loginMessageDto);
        }else{
            return new CommonResult(LoginEnum.USER_NULL.getCode(),LoginEnum.PASSWORD_ERROR.getMessage(),loginMessageDto);
        }

    }

    @ApiOperation(value = "退出登录",httpMethod = "GET")
    @GetMapping("/exit")
    public CommonResult exit(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if (session==null){
            return new CommonResult(ResultCode.ERROR.getCode(),"您还没登录",null);
        }
        session.removeAttribute("userbean");
        return new CommonResult(ResultCode.SUCCESS.getCode(),"注销成功",null);
    }

    @ApiOperation(value = "增加用户/不是注册不是网站(管理员的权限)",httpMethod = "POST")
    @PostMapping("/addUser")
    public CommonResult addUser(HttpServletRequest request, @RequestBody User user){
        User userbean=(User) request.getSession().getAttribute("userbean");
        switch (userService.insert(userbean,user)){
            case 0:
                return new CommonResult(ResultCode.ERROR.getCode(),"权限不足",null);
            case 1:
                return new CommonResult(ResultCode.SUCCESS.getCode(),"增加成功",null);
            case 2:
                return new CommonResult(ResultCode.ERROR.getCode(),"已有此用户名",null);
            default:
                return new CommonResult(ResultCode.ERROR.getCode(),"后端报错",null);
        }
    }

    @ApiOperation(value = "增加用户/不是注册不是网站(管理员的权限)",httpMethod = "GET")
    @GetMapping("/deleteUser")
    public CommonResult deleteUser(HttpServletRequest request,Long userid){
        User userbean=(User) request.getSession().getAttribute("userbean");
        switch (userService.deleteUser(userbean,userid)){
            case 0:
                return new CommonResult(ResultCode.ERROR.getCode(),"权限不足",null);
            case 1:
                return new CommonResult(ResultCode.SUCCESS.getCode(),"删除成功",null);
            case 2:
                return new CommonResult(ResultCode.ERROR.getCode(),"没有此人",null);
            default:
                return new CommonResult(ResultCode.ERROR.getCode(),"后台报错",null);
        }

    }

}
